package com.example.appinting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.appinting.Beans.appoint;
import com.example.appinting.Beans.doctor;
import com.example.appinting.Fragment.Appoint;
import com.example.appinting.Fragment.Index;
import com.example.appinting.Fragment.Me;
import com.example.appinting.SQL.MysqlHelper;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements  View.OnClickListener{
    private ImageView index,me,myappoint;

    private FrameLayout ly_content;

    private Appoint f1;
    private Index f0;
    private Me f2;

    private String patient_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Intent intent = getIntent();
        patient_id = intent.getStringExtra("patient_id");


        setContentView(R.layout.activity_index);
        bindView();
        selectTab(0);

    }


    //UI组件初始化与事件绑定
    private void bindView() {
        index = (ImageView)this.findViewById(R.id.index);
        myappoint = (ImageView)this.findViewById(R.id.myappoint);
        me = (ImageView)this.findViewById(R.id.me);

        ly_content = (FrameLayout) findViewById(R.id.fragment_container);

        index.setOnClickListener(this);
        myappoint.setOnClickListener(this);
        me.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.index) {
            selectTab(0);
        } else if (v.getId() == R.id.myappoint) {
            selectTab(1);
        } else if (v.getId() == R.id.me) {
            selectTab(2);
        }
    }


    //隐藏所有Fragment
    public void hideAllFragment(FragmentTransaction transaction){
        if(f0!=null){
            index.setImageResource(R.drawable.index);
            transaction.hide(f0);
        }
        if(f1!=null){
            myappoint.setImageResource(R.drawable.pay);
            transaction.hide(f1);
        }
        if(f2!=null){
            me.setImageResource(R.drawable.me_icon);
            transaction.hide(f2);
        }
    }

    //处理Tab的点击事件

    //进行选中Tab的处理
    private void selectTab(int i) {
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideAllFragment(transaction);
        switch (i) {
            case 0:
                if (f0 == null) {
                    index.setImageResource(R.drawable.index2);

                    List<doctor> doctors = queryData();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("doctorList", (Serializable) doctors);
                    f0 = new Index();
                    f0.setArguments(bundle);

                    transaction.add(R.id.fragment_container, f0);


//                    transaction.add(R.id.fragment_container, f0);
                } else {
                    index.setImageResource(R.drawable.index2);
                    //为的是重新读取数据库数据
                    transaction.remove(f0);

                    List<doctor> doctors = queryData();
//                    System.out.println(doctors.get(0).getDepartment());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("doctorList", (Serializable) doctors);
                    f0 = new Index();
                    f0.setArguments(bundle);

                    transaction.add(R.id.fragment_container, f0);
                    transaction.show(f0);
                }
                break;
            //当选中点击的是第一页的Tab时
            case 1:
                //如果第一页对应的Fragment没有实例化，则进行实例化，并显示出来
                if (f1 == null) {
                    myappoint.setImageResource(R.drawable.appoint2);

                    List<appoint> appoints = queryAppoint();
                    Bundle bundle = new Bundle();

                    System.out.println("test20");
                    System.out.println(appoints.get(0).getPatient_id());
                    bundle.putSerializable("appointList", (Serializable) appoints);
                    f1 = new Appoint();
                    f1.setArguments(bundle);

                    transaction.add(R.id.fragment_container, f1);
                } else {
                    myappoint.setImageResource(R.drawable.appoint2);
                    transaction.remove(f1);

                    List<appoint> appoints = queryAppoint();
                    System.out.println("test1");
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("appointList", (Serializable) appoints);
                    f1 = new Appoint();
                    f1.setArguments(bundle);

                    transaction.add(R.id.fragment_container, f1);
                    //如果第一页对应的Fragment已经实例化，则直接显示出来
                    transaction.show(f1);
                }
                break;


            case 2:
                if (f2 == null) {
                    me.setImageResource(R.drawable.me2);
                    f2 = new Me();
                    transaction.add(R.id.fragment_container, f2);
                } else {
                    me.setImageResource(R.drawable.me2);
                    transaction.remove(f2);
                    f2 = new Me();
                    transaction.add(R.id.fragment_container, f2);
                    transaction.show(f2);
                }
                break;
        }
        //不要忘记提交事务
        transaction.commit();
    }

    private List<doctor> queryData() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<doctor> doctors = new ArrayList<>();

        try {
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "SELECT * FROM doctor";
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            resultSet = statement.executeQuery();

            // 处理查询结果
            while (resultSet.next()) {
                int doctor_id = resultSet.getInt("doctor_id");
                String doctor_name = resultSet.getString("doctor_name");
                String department = resultSet.getString("doctor_department");
                String phone_number = resultSet.getString("phone_number");
                doctors.add(new doctor(doctor_id,doctor_name, phone_number,department));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return doctors;
    }

    private List<appoint> queryAppoint() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<appoint> appoints = new ArrayList<>();

        try {
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "SELECT r.id,r.doctor_id,r.patient_id, r.appoint_state,r.period_id, d.doctor_name, pe.start_time, pe.end_time, d.doctor_department " +
                    "FROM reservation r " +
                    "INNER JOIN doctor d ON d.doctor_id = r.doctor_id " +
                    "INNER JOIN period pe ON pe.period_id = r.period_id WHERE r.patient_id = " + patient_id;
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            resultSet = statement.executeQuery();

            // 处理查询结果
            while (resultSet.next()) {
                int appoint_id = resultSet.getInt("id");
                int doctor_id = resultSet.getInt("doctor_id");
                int patient_id = resultSet.getInt("patient_id");
                int appoint_state = resultSet.getInt("appoint_state");
                int period_id = resultSet.getInt("period_id");
                String doctor_name = resultSet.getString("doctor_name");
                String start_time = resultSet.getString("start_time");
                String end_time = resultSet.getString("end_time");
                String department = resultSet.getString("doctor_department");
                appoints.add(new appoint(appoint_id,doctor_id,patient_id,appoint_state,period_id,doctor_name,start_time, end_time, department));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return appoints;
    }
}






